var status = 0;
var selectedList = Array();
var newItemList = Array();
var itemBorder = "#fUI/UIWindow.img/Item/New/inventory/0#";
var itemMaster = "#fUI/UIWindow.img/Item/activeExpChairIcon#";
var itemIcon = "#fUI/Basic.img/Cursor/32/0#";
var numArr = Array("#fUI/Basic.img/LevelNo/0#", "#fUI/Basic.img/LevelNo/1#", "#fUI/Basic.img/LevelNo/2#", "#fUI/Basic.img/LevelNo/3#", "#fUI/Basic.img/LevelNo/4#", "#fUI/Basic.img/LevelNo/5#", "#fUI/Basic.img/LevelNo/6#", "#fUI/Basic.img/LevelNo/7#", "#fUI/Basic.img/LevelNo/8#", "#fUI/Basic.img/LevelNo/9#");
var btnOk = "#fUI/CashShop.img/CSCoupon/BtOK/normal/0#";
var btnOk_disabled = "#fUI/CashShop.img/CSCoupon/BtOK/disabled/0#";
var startIcon = "#fUI/Basic.img/icon/arrow#";
var selectedPosition = 0;
var step = 0;
var successRate = 0;
var cost = 0;
var haveLuck = false;
var useLuck = false;
var sflag = false;
var grade = Array("★普通★", "★普通★", "★精致★", "★精致★", "★无暇★", "★无暇★", "★灵动★", "★纯洁★", "★完美★", "★神圣★", "★神圣★", "★神圣★");

function start() {
    status = -1;
    action(1, 0, 0)
}

function action(m, d, z) {
    if (m == -1) {
        cm.dispose()
    } else {
        if (haveLuck && m == 0) {
            useLuck = false;
            status = 0;
            m = 1
        } else {
            if (haveLuck && m == 1) {
                useLuck = true
            }
        }
        if (m == 0 && status == 0) {
            cm.dispose();
            return
        }
        if (m == 0 && status == -1) {
            cm.dispose();
            return
        }
        if (m == 1) {
            status++
        } else {
            status--
        }
        if (status == 0) {
            if (step == 1) {
                if (selectedPosition == 0) {
                    selectedList.splice(0, 4)
                }
                if (z != -1) {
                    selectedList[selectedPosition] = Array(z, newItemList[z])
                }
                step = 0;
                successRate = getSuccessRate()
            }
            var l = "#e┌\t─ " + itemIcon + "神秘力量强化 1次可合成5个─\t\t┐#n\r\n\r\n";
            for (var r = 0; r < 5; r++) {
                if (selectedList[r] != null) {
                    l += "#L" + r + "##v" + selectedList[r][1] + "##z" + selectedList[r][1] + "##l\r\n"
                } else {
                    if (r == 0) {
                        if (selectedList.length < 1) {
                            l += "#L" + r + "#" + itemMaster + "#l请点击框框选择主装备\r\n"
                        } else {
                            l += "#L" + r + "#" + itemMaster + "#l\r\n"
                        }
                    } else {
                        if (selectedList.length < 1) {
                            l += "#L" + r + "#" + itemBorder + "#l请点击框框选择副装备\r\n"
                        } else {
                            l += "#L" + r + "#" + itemBorder + "#l\r\n"
                        }
                    }
                }
            }
            l += "#e\r\n\r\n\r\n└\t\t\t\t\t\t\t\t\t\t\t┘#n";
            if (selectedList.length >= 1) {
                l += "#k\r\n#e┌\t\t     ─ 已经选择的装备信息 ─   \t\t┐#n\r\n\r\n";
                for (var y in selectedList) {
                    var v = cm.getInventory(1).getItem(selectedList[y][0]);
                    var a = v.getTitle();
                    var q = 0;
                    for (var r = 0; r < grade.length; r++) {
                        if (a == grade[r]) {
                            break
                        }
                        q++
                    }
                    if (q >= grade.length) {
                        a = grade[0]
                    }
                    var j = "主";
                    if (y >= 1) {
                        j = "#k副"
                    }
                    var n = v.getLevel();
                    var g = "";
                    if (n != 0) {
                        g = " (+" + n + ")"
                    }
                    l += "\t" + j + ": #r[" + a + "]#n 神秘力量." + v.getArc().getPower() + " #d#e" + cm.getItemName(v.getItemId()) + "#n" + g + "\r\n 攻击 " + v.getPAD() + "\r\n"
                }
                l += "#e\r\n└\t\t\t\t\t\t\t\t\t\t\t┘#n"
            }
            var s = btnOk_disabled;
            if (selectedList.length >= 2) {
                s = btnOk
            }
            l += "#k\t\t\t\t#L999##d#e" + s + "#l\r\n\r\n";
            l += "#k\r\n#e┌\t\t\t     ─ 操作帮助 ─   \t\t\t┐#n\r\n";
            l += "\t#b" + numArr[1] + " 合成前，请先仔细阅读合成说明。\r\n\t" + numArr[2] + " 第一个位置选择需要提升品级的主装备。\r\n\t#r" + numArr[3] + " 如果主装备变动，副装备需要重新选择。\r\n\t" + numArr[4] + " 选择装备时，装备的排列顺序是依据背包里的顺序。\r\n\t" + numArr[5] + " 选择结束后，点击“确认”进行装备合成#k";
            l += "#e\r\n└\t\t\t\t\t\t\t\t\t\t\t┘#n";
            cm.askMenu(l)
        } else {
            if (status == 1) {
                if (sflag) {
                    z = 999
                }
                if (z == 999) {
                    sflag = true;
                    if (selectedList.length < 0) {
                        cm.sendPrev("无法合成，请至少放入一件副装备")
                    } else {
                        if (cm.haveItem(4000517) && !haveLuck) {
                            status = 0;
                            haveLuck = true;
                            cm.askYesNo("您的背包中拥有#v4000517##b黄金鱼#k道具，是否使用#b黄金鱼#k将成功率提升至#b#e" + (successRate + 15) + "%#n#k？\r\n\r\n#d#e选择否则以#r" + successRate + "%#d的成功率继续强化。#n#k")
                        } else {
                            if (cm.getPlayer().getCSPoints(1) < 1000) {
                                cm.sendOk("您的点卷不足");
                                cm.dispose();
                                return
                            }
                            var f = selectedList[0][1];
                            var p = selectedList[0][0];
                            var t = cm.getReqLevel(f);
                            var e = Math.floor(getGrade(p)) + 1;
                            var v = cm.getInventory(1).getItem(p);
                            if (e > (10)) {
                                cm.sendOk("您的装备已经达到最高品级，无法再进行合成。");
                                cm.dispose();
                                return
                            }
                            cm.getPlayer().modifyCSPoints(1, -1000);
                            var c = Math.floor(Math.random() * 100);
                            successRate = (useLuck) ? successRate + 15 : successRate;
                            if (useLuck) {
                                cm.gainItem(4000517, -1)
                            }
                            var v = cm.getInventory(1).getItem(p);
                            var k = cm.getItemInfo();
                            var h = v.copy();
                            var w = e * Math.round((t / 30));
                            if (v.getArc().getLevel() == 20) {
                                cm.sendOk("合成失败,你的神秘力量等级已是最高的了");
                                cm.dispose();
                                return
                            }
                            h.setTitle("" + grade[e] + "");
                            h.setStr(v.getStr() + e * 10 * selectedList.length);
                            h.setDex(v.getDex() + e * 10 * selectedList.length);
                            h.setInt(v.getInt() + e * 10 * selectedList.length);
                            h.setLuk(v.getLuk() + e * 10 * selectedList.length);
                            h.setPAD(v.getPAD() + e * 10 * selectedList.length);
                            h.setMAD(v.getMAD() + e * 10 * selectedList.length);
                            h.getArc().setPower(v.getArc().getPower() + 30 * selectedList.length);
                            h.getArc().setLevel(v.getArc().getLevel() + 1 * selectedList.length);
                            for (var y in selectedList) {
                                cm.removeSlot(1, selectedList[y][0], 1)
                            }
                            cm.addFromDrop(cm.getClient(), h, false);
                            var l = "#b力量#r+" + e * 10 * selectedList.length + "\r\n";
                            l += "#b敏捷#r+" + e * 10 * selectedList.length + "\r\n";
                            l += "#b智力#r+" + e * 10 * selectedList.length + "\r\n";
                            l += "#b运气#r+" + e * 10 * selectedList.length + "\r\n";
                            l += "#b攻击力#r+" + e * 10 * selectedList.length + "\r\n";
                            l += "#b魔法力#r+" + e * 10 * selectedList.length + "\r\n";
                            cm.sendOk("#r#e强化成功！#n#k本次强化为您的装备#d[#v" + f + "#]#k提升了\r\n#k" + l);
                            sflag = false;
                            if (e >= 3) {
                                cm.worldMessage("[装备合成] : 恭喜[" + cm.getPlayer().getName() + "]合成出" + grade[e] + "的 " + cm.getItemName(f))
                            }
                            cm.worldSpouseMessage(21, "[装备合成] : 恭喜 " + cm.getPlayer().getName() + " 合成出 " + grade[e] + "的 " + cm.getItemName(f));
                            cm.dispose()
                        }
                    }
                } else {
                    selectedPosition = z;
                    if (selectedPosition != 0 && selectedList[0] == null) {
                        cm.sendPrev("请先选择主装备！")
                    } else {
                        inventoryType = 1;
                        var u = cm.getInventory(inventoryType).list();
                        var o = u.iterator();
                        l = "#e经过筛选，以下为所有符合强化合成条件的#r副装备#n\r\n\r\n#b";
                        if (selectedPosition == 0) {
                            l = "#e#d请选择需要进行强化合成的#r主装备：#n\r\n\r\n#b"
                        }
                        var x = 1;
                        newItemList = Array();
                        while (o.hasNext()) {
                            var v = o.next();
                            if (cm.isCash(v.getItemId())) {
                                continue
                            }
                            if (getItemType(v.getItemId()) == -1) {
                                continue
                            }
                            var b = cm.getReqLevel(v.getItemId());
                            if (b < 50) {
                                continue
                            }
                            var b = cm.getReqLevel(v.getItemId());
                            if (b > 200) {
                                continue
                            }
                            if ((v.getArc().getPower() < 1)) {
                                continue
                            }
                            var q = 0;
                            for (var y in selectedList) {
                                if (v.getPosition() == selectedList[y][0]) {
                                    q = 1;
                                    break
                                }
                            }
                            if (q == 1) {
                                continue
                            }
                            newItemList[v.getPosition()] = v.getItemId()
                        }
                        for (var y in newItemList) {
                            l += "#L" + y + "##v" + newItemList[y] + "##z" + newItemList[y] + "#";
                            if (x > 1 && x % 5 == 0) {
                                l += "\r\n"
                            }
                            x++
                        }
                        status = -1;
                        step = 1;
                        if (newItemList.length < 0) {
                            l = "#r没有可以进行合成的副装。#k";
                            cm.dispose();
                            cm.askMenu(l)
                        } else {
                            cm.askMenu(l)
                        }
                    }
                }
            }
        }
    }
}

function getItemType(b) {
    var a = Math.floor(b / 10000);
    switch (a) {
        case 100:
            return 0;
        case 104:
            return 1;
        case 105:
            return 2;
        case 106:
            return 3;
        case 107:
            return 4;
        case 108:
            return 5;
        case 110:
            return 6;
        default:
            if (a == 120) {
                return -1
            }
            if (a == 135) {
                return -1
            }
            var a = Math.floor(a / 10);
            if (a == 12 || a == 13 || a == 14 || a == 15 || a == 17) {
                return 7
            }
            return -1
    }
}

function getSuccessRate() {
    var b = 0;
    for (var a in selectedList) {
        if (selectedList[a] != null && selectedList[a] != "") {
            b++
        }
    }
    switch (b) {
        case 2:
            return 100;
        case 3:
            return 36;
        case 4:
            return 58;
        case 5:
            return 85;
        default:
            return 0
    }
}

function getCost() {
    var b = 0;
    for (var c in selectedList) {
        b += cm.getReqLevel(selectedList[c][1]) * 1
    }
    var a = (b) + cm.getReqLevel(selectedList[0][1]) * (parseInt(getGrade(selectedList[0][0])) + 10);
    return a
}

function getGrade(b) {
    if (b != null) {
        var c = cm.getInventory(1).getItem(b);
        var d = c.getTitle();
        if (d == null || d == "") {
            return 0
        }
        for (var a in grade) {
            if (d == grade[a]) {
                return a
            }
        }
    }
    return 0
};